/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.server.http;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tunnel.common.tunnel.TunnelDataSenderManager;
import com.tunnel.server.http.HttpClientManager.HttpClient;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;

public class HttpChannelManager {
	public static class HttpChannelContext{
		private String host;
		private String requestId;
		private ChannelHandlerContext channel;
		private ChannelFuture channelFuture;
		
		public String getHost() {
			return host;
		}
		public void setHost(String host) {
			this.host = host;
		}
		public String getRequestId() {
			return requestId;
		}
		public void setRequestId(String requestId) {
			this.requestId = requestId;
		}
		public ChannelHandlerContext getChannel() {
			return channel;
		}
		public void setChannel(ChannelHandlerContext channel) {
			this.channel = channel;
		}
		public ChannelFuture getChannelFuture() {
			return channelFuture;
		}
		public void setChannelFuture(ChannelFuture channelFuture) {
			this.channelFuture = channelFuture;
		}
		
	}
	
	private static Map<String,HttpChannelContext> CHANNEL_MAP = new HashMap<>();
	
	public synchronized static boolean add(String host,String requestId,ChannelHandlerContext ctx){
		if(CHANNEL_MAP.containsKey(requestId)){
			return false;
		}
		
//		LogUtil.d("新的请求: "+requestId);
		HttpChannelContext httpCtx = new HttpChannelContext();
		httpCtx.setHost(host);
		httpCtx.setRequestId(requestId);
		httpCtx.setChannel(ctx);
		CHANNEL_MAP.put(requestId, httpCtx);
		return true;
	}
	
	public synchronized static HttpChannelContext get(String requestId){
		return CHANNEL_MAP.get(requestId);
	}

	public synchronized static List<HttpChannelContext> getList(String host){
		Collection<HttpChannelContext> values = CHANNEL_MAP.values();
		List<HttpChannelContext> list = new ArrayList<>();
		for(HttpChannelContext ctx:values){
			if(ctx.getHost() == host){
				list.add(ctx);
			}
		}
		return list;
	}
	
	public static void remove(String requestId) {
//		LogUtil.d("断开http请求: "+requestId);
		HttpChannelContext ctx = CHANNEL_MAP.remove(requestId);
		
		//通知tunnel-client，不需要再继续传递数据给这个requestId
		if(ctx != null){
			HttpClient httpClient = HttpClientManager.get(ctx.getHost());
			if(httpClient != null){//连接还在
				ChannelHandlerContext clientCtx = httpClient.getChannelHandlerContext();
				byte[] requestIdBytes = requestId.getBytes();
				TunnelDataSenderManager.commitData(TunnelDataSenderManager.STOP_HTTP_TASK_MSG, clientCtx, requestIdBytes);
			}
		}
		
	}
}
